package com.hopes.bizz.close.visitor.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hopes.bizz.close.basic.entity.ClosedGeneralCarEntity;
import com.hopes.bizz.close.basic.entity.ClosedPersonCompanyEntity;
import com.hopes.bizz.close.basic.service.ClosedGeneralCarService;
import com.hopes.bizz.close.basic.service.ClosedPersonCompanyService;
import com.hopes.bizz.close.basic.service.ClosedPersonService;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.HazardDicConstant;
import com.hopes.bizz.close.common.constant.VisitorDicConstant;
import com.hopes.bizz.close.hazard.entity.ClosedHazardCarEntity;
import com.hopes.bizz.close.hazard.service.ClosedHazardCarService;
import com.hopes.bizz.close.visitor.entity.ClosedHazardCarApplyEntity;
import com.hopes.bizz.close.visitor.entity.ClosedVisitorApplyEntity;
import com.hopes.bizz.close.visitor.service.ClosedAppVisitorService;
import com.hopes.bizz.close.visitor.service.ClosedHazardCarApplyService;
import com.hopes.bizz.close.visitor.service.ClosedVisitorApplyService;
import com.hopes.bizz.close.visitor.vo.ClosedApplyToDoAuditVO;
import com.hopes.boss.common.entity.BaseEntity;
import com.hopes.boss.common.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 移动端相关接口
 * @time 2024/2/5 14:27
 */
@Service
@RequiredArgsConstructor
public class ClosedAppVisitorServiceImpl implements ClosedAppVisitorService {

    private final ClosedHazardCarService closedHazardCarService;
    private final ClosedGeneralCarService closedGeneralCarService;
    private final ClosedVisitorApplyService closedVisitorApplyService;
    private final ClosedPersonCompanyService closedPersonCompanyService;
    private final ClosedHazardCarApplyService closedHazardCarApplyService;

    @Override
    public ClosedApplyToDoAuditVO queryAppApplyTodoAudit() {
        final Integer companyPersonNum = countNum(closedPersonCompanyService, buildQueryWrapper(ClosedPersonCompanyEntity::getOrgId, ClosedPersonCompanyEntity::getAuditStatus, Collections.singletonList(BasicDicConstant.CLOSE_PERSON_AUDIT_STATUS_NOT_AUDIT)));
        final Integer hazardCarNum = countNum(closedHazardCarService, buildQueryWrapper(ClosedHazardCarEntity::getOrgId, ClosedHazardCarEntity::getAuditStatus, Collections.singletonList(HazardDicConstant.CLOSE_HAZARD_CAR_AUDIT_STATUS_NOT_AUDIT)));
        final Integer generalCarNum = countNum(closedGeneralCarService, buildQueryWrapper(ClosedGeneralCarEntity::getOrgId, ClosedGeneralCarEntity::getAuditStatus, Collections.singletonList(BasicDicConstant.CLOSE_GENERAL_CAR_AUDIT_STATUS_NOT_AUDIT)));
        final Integer visitorApplyNum = countNum(closedVisitorApplyService, buildQueryWrapper(ClosedVisitorApplyEntity::getOrgId, ClosedVisitorApplyEntity::getAuditStatus, Collections.singletonList(VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_AUDIT)));
        final Integer hazardCarApplyNum = countNum(closedHazardCarApplyService, buildQueryWrapper(ClosedHazardCarApplyEntity::getOrgId, ClosedHazardCarApplyEntity::getAuditStatus, Collections.singletonList(VisitorDicConstant.CLOSE_VISITOR_AUDIT_STATUS_NOT_AUDIT)));
        return new ClosedApplyToDoAuditVO(visitorApplyNum, hazardCarNum, generalCarNum, hazardCarApplyNum, companyPersonNum);
    }


    private <T extends BaseEntity> Integer countNum(IService<T> service, LambdaQueryWrapper<T> queryWrapper ) {
        return Long.valueOf(service.count(queryWrapper)).intValue();
    }

    private <T> LambdaQueryWrapper<T> buildQueryWrapper(SFunction<T, Long> orgMapper, SFunction<T, Integer> auditStatusMapper, List<Integer> auditStatusList) {
        final Long orgId = SecurityUtils.getOrgId();
        return Wrappers.<T>lambdaQuery().eq(ObjUtil.isNotNull(orgId), orgMapper, orgId).in(CollUtil.isNotEmpty(auditStatusList), auditStatusMapper, auditStatusList);
    }
}
